package com.itoffer.web;

import com.itoffer.pojo.*;
import com.itoffer.service.CompanyService;
import com.itoffer.service.impl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/company/*")
public class recruitServlet extends BaseServlet {
    private CompanyService companyService = new CompanyServiceImpl();

    /**
     * 通过公司ID查询公司信息,以及职位信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectByCompany(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传过来的公司id
        String _company_id = req.getParameter("company_id");
        int company_id = Integer.parseInt(_company_id);

        //        调用业务层方法
        Company company = companyService.selectByCompanyId(company_id);
        Job job = companyService.selectJobByCompanyId(company_id);
//        System.out.println(company);
//        System.out.println(job);
        //将查询出来的数据转发到Company.jsp
        if (company != null && job != null) {
            req.setAttribute("company", company);
            req.setAttribute("job", job);
            req.getRequestDispatcher("/recruit/company.jsp").forward(req, resp);
        } else {
            String contextPath = req.getContextPath();
            resp.sendRedirect(contextPath + "/error.jsp");
        }

    }

    /**
     * 选择公司申请职位
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void addJobApply(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传来的数据申请人applicant_id,要申请的job_id,以及申请的时间
        String _job_id = req.getParameter("job_id");
        int job_id = Integer.parseInt(_job_id);
        HttpSession session = req.getSession();
        Applicant applicant = (Applicant) session.getAttribute("applicant");
        int applicant_id = applicant.getApplicant_id();
        String applytime = req.getParameter("applytime");
        //System.out.println(applytime);
        //调用业务层
        boolean b = companyService.addJobApply(job_id, applicant_id, applytime);
        //System.out.println(b);
        if (b) {
            CompanyJobName companyJobName = companyService.selectCompanyAndJobNameByJObId(job_id);
            //System.out.println(companyJobName);
            HttpSession session1 = req.getSession();
            session1.setAttribute("job_name", companyJobName.getJob_name());
            session1.setAttribute("company_name", companyJobName.getCompany_name());
            String contextPath = req.getContextPath();
            resp.sendRedirect(contextPath + "/recruit/applysuccess.jsp");
        } else {
            String contextPath = req.getContextPath();
            resp.sendRedirect(contextPath + "/error.jsp");
        }
    }

    /**
     * 查询个人申请的公司以及职位信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectJobApply(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Applicant applicant = (Applicant) session.getAttribute("applicant");
        //调用业务层方法
        List<JobApply> jobApplies = companyService.selectJobApplyByApplicantId(applicant.getApplicant_id());
        //测试
        //System.out.println(jobApplies);
        if(jobApplies.size()>0){
            List<ApplyS> jobApplyList = new ArrayList();
            for (int i=0;i<jobApplies.size();i++){
                ApplyS applyS = new ApplyS();
                //调用查询企业名字工作职位方法
                CompanyJobName companyJobName = companyService.selectCompanyAndJobNameByJObId(jobApplies.get(i).getJob_id());
                applyS.setApply_id(jobApplies.get(i).getApply_id());
                applyS.setCompany_name(companyJobName.getCompany_name());
                applyS.setJob_name(companyJobName.getJob_name());
                applyS.setApply_state(jobApplies.get(i).getApply_state());
                jobApplyList.add(applyS);
            }
            System.out.println(jobApplyList);
            req.setAttribute("jobApplyList",jobApplyList);
            req.getRequestDispatcher("/applicant/jobApply.jsp").forward(req,resp);
        }else {
            req.setAttribute("msg","未查询到申请职位申请信息，请先申请职位");
            req.getRequestDispatcher("../error.jsp").forward(req,resp);
        }

    }

    /**
     * 删除个人申请
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void deleteJobByApplyId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String _apply_id = req.getParameter("apply_id");
        int apply_id = Integer.parseInt(_apply_id);

        //调用删除方法
        boolean b = companyService.deleteApplyByApplyId(apply_id);
        if(b){
            //System.out.println("删除成功");
            //刷新页面
            HttpSession session = req.getSession();
            Applicant applicant = (Applicant) session.getAttribute("applicant");
            //调用业务层方法
            List<JobApply> jobApplies = companyService.selectJobApplyByApplicantId(applicant.getApplicant_id());
            //测试
            //System.out.println(jobApplies);
            if(jobApplies.size()>0){
                List<ApplyS> jobApplyList = new ArrayList();
                for (int i=0;i<jobApplies.size();i++){
                    ApplyS applyS = new ApplyS();
                    //调用查询企业名字工作职位方法
                    CompanyJobName companyJobName = companyService.selectCompanyAndJobNameByJObId(jobApplies.get(i).getJob_id());
                    applyS.setApply_id(jobApplies.get(i).getApply_id());
                    applyS.setCompany_name(companyJobName.getCompany_name());
                    applyS.setJob_name(companyJobName.getJob_name());
                    applyS.setApply_state(jobApplies.get(i).getApply_state());
                    jobApplyList.add(applyS);
                }
                //System.out.println(jobApplyList);
                req.setAttribute("jobApplyList",jobApplyList);
                req.getRequestDispatcher("/applicant/jobApply.jsp").forward(req,resp);
            }else {
                req.setAttribute("msg","未查询到申请职位申请信息，请先申请职位");
                req.getRequestDispatcher("../error.jsp").forward(req,resp);
            }
        }else {
            //System.out.println("删除失败");
        }
    }

    /**
     * 批量删除个人申请
     */
    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String arrays = req.getParameter("arrays");
        String[] strings = arrays.split(",");
        int[] array = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            array[i] = Integer.parseInt(strings[i]);
        }
        companyService.deleteByApplyIds(array);
        req.setAttribute("msg","未查询到申请职位申请信息，请先申请职位");
        req.getRequestDispatcher("../error.jsp").forward(req,resp);
    }

    /**
     * 分页查询转发
     */
    public void selectByPage(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        //获取jsp传来的页码，当前页码和每页展示数据
        String _currentPage = req.getParameter("currentPage");
        if(_currentPage ==null){
            _currentPage="1";
        }
        int currentPage = Integer.parseInt(_currentPage);
        //获取session中的applicant_id
        HttpSession session = req.getSession();
        Applicant applicant = (Applicant) session.getAttribute("applicant");
        //调用业务层方法
        PageBean<JobApply> pageBean =companyService.selectByPage(applicant.getApplicant_id(),currentPage,3);
        List<JobApply> list = pageBean.getRows();
        //总条目数
        int totalCount = pageBean.getTotalCount();
        //展示的总页数，不足一页加一页
        int pages;
        if(totalCount%3==0){
            pages = totalCount/3;
        }else{
            pages = (totalCount/3)+1;
        }
        //分发到对应页面
        if(list.size()>0) {
            List<ApplyS> jobApplyList = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                ApplyS applyS = new ApplyS();
                //调用查询企业名字工作职位方法
                CompanyJobName companyJobName = companyService.selectCompanyAndJobNameByJObId(list.get(i).getJob_id());
                applyS.setApply_id(list.get(i).getApply_id());
                applyS.setCompany_name(companyJobName.getCompany_name());
                applyS.setJob_name(companyJobName.getJob_name());
                applyS.setApply_state(list.get(i).getApply_state());
                jobApplyList.add(applyS);
            }
//            System.out.println(jobApplyList);
            req.setAttribute("jobApplyList", jobApplyList);
            req.setAttribute("pages", pages);
            req.getRequestDispatcher("/applicant/jobApply.jsp").forward(req, resp);

        }else {
            req.setAttribute("msg","未查询到申请职位申请信息，请先申请职位");
            req.getRequestDispatcher("../error.jsp").forward(req,resp);
        }
    }
}
